<?php
require_once "E:/php_project/php_demo/tushare/common/http.php";
require_once "E:/php_project/php_demo/tushare/common/mysql.php";
set_time_limit(0);
$conn = new MysqlCommon();
$stockRes = $conn->select("SELECT * FROM tushare.stock_basic");

foreach ($stockRes as $stockResItem){
    $ts_code = $stockResItem["ts_code"];

    $url = "http://api.tushare.pro";
//{"api_name": "stock_basic", "token": "7a37dc57bcb3309fbc47fda4fcf44c7844d3a506debb4323fb6738af"}
    $data = array(
        "api_name"=>"daily",
        "token"=>"7a37dc57bcb3309fbc47fda4fcf44c7844d3a506debb4323fb6738af",
        "params"=>array(
            "ts_code"=>"{$ts_code}",
            "start_date"=>"20220601",
            "end_date"=>"20220812",
        ),
        "fields"=>"ts_code,trade_date,open,high,low,close,pre_close,change,pct_chg,vol,amount",
    );
    $res = Http::post($url,$data);
    $resData = json_decode($res,true);
    foreach ($resData["data"]["items"] as $item){
        $sql = "INSERT INTO `tushare`.`daily`(`ts_code`,`trade_date`, `open`, `high`, `low`, `close`, `pre_close`, `change`, `pct_chg`, `vol`, `amount`) 
VALUES ('{$item[0]}', {$item[1]}, {$item[2]}, {$item[3]}, {$item[4]}, {$item[5]}, {$item[6]}, {$item[7]}, {$item[8]}, {$item[9]},{$item[10]})";
        $conn->insert($sql);
    }
}

echo "完成";
exit(0);



